#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include "idc_ubus.h"
#include "idc_ubus_beidou.h"

static const struct ubus_method idc_methods[] = {
        UBUS_METHOD("location", location, default_policy),
        UBUS_METHOD("cmd_ctl", cmd_ctl, beidou_cmd_ctl_policy),
        UBUS_METHOD("save_cfg", save_cfg, default_policy),
        //UBUS_METHOD("test1", test1, default_policy),
};
 
static struct ubus_object_type idc_object_type =  UBUS_OBJECT_TYPE("sihan.beidou", idc_methods);
 
 
static struct ubus_object idc_object = {
        .name = "sihan.beidou",
        .type = &idc_object_type,
        .methods = idc_methods,
        .n_methods = ARRAY_SIZE(idc_methods)
};
 
int ubus_doing()
{
        int ret;
 
        ctx = ubus_connect(NULL);
        if (!ctx) {
                fprintf(stderr, "Failed to connect to ubus\n");
                return -1;
        }
        ubus_add_uloop(ctx);
 
        ret = ubus_add_object(ctx, &idc_object);
        if (ret)
                fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
        #if 0
        ret = ubus_register_subscriber(ctx, &test_event);
	if (ret)
		fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
        #endif
}
void *ubus_worker(void *arg)
{
        int ret;
        printf("idc_ubus_init\n");
        uloop_init();
        ubus_doing();
        uloop_run();
 
        ubus_free(ctx);
        uloop_done();    
}

void ubus_worker_start(void)
{
        pthread_t pid;
        pthread_create(&pid, NULL, (void *)ubus_worker, NULL);
}
